package com.atyoung.youngmall.product.service.impl;

import com.atyoung.youngmall.product.dao.CategoryBrandRelationDao;
import com.atyoung.youngmall.product.entity.CategoryBrandRelationEntity;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atyoung.common.utils.PageUtils;
import com.atyoung.common.utils.Query;

import com.atyoung.youngmall.product.dao.CategoryDao;
import com.atyoung.youngmall.product.entity.CategoryEntity;
import com.atyoung.youngmall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> categoryEntities =  categoryDao.selectList(null);

        List<CategoryEntity> leverMenus = categoryEntities.stream().filter(categoryEntity -> {
           return categoryEntity.getParentCid() == 0;
        }).peek(menu -> {
            menu.setChildren(getChildren(menu,categoryEntities));
        }).collect(Collectors.toList());
        return leverMenus;
    }

    @Override
    public void removMenusByIds(Long[] catIds) {
        //检查当前的菜单是否被引用
        categoryDao.deleteBatchIds(Arrays.asList(catIds));
    }

    @Override
    public List<Long> getCategoryTreeNode(Long catId) {
        List<Long>  catIdList = new ArrayList<>();
        catIdList.add(catId);
        getPath(catIdList,catId);
        System.out.println(catIdList);
        return catIdList;
    }

    @Transactional
    @Override
    public void updateDetial(CategoryEntity category) {
        this.baseMapper.updateById(category);
        //更新冗余字段
        Long catId = category.getCatId();
        String categoryName = category.getName();
        if(StringUtils.isNotEmpty(categoryName)){
            categoryBrandRelationDao.updateDetail(catId,categoryName);

        }
    }

    private List<Long> getPath(List<Long>  catIdList , Long catId){
        CategoryEntity categoryEntity = categoryDao.selectById(catId);
        if(categoryEntity.getParentCid() != 0) {
            catIdList.add(categoryEntity.getParentCid());
            getPath(catIdList, categoryEntity.getParentCid());
        }
        return catIdList;
    }

    private List<CategoryEntity> getChildren(CategoryEntity menu,List<CategoryEntity>  categoryEntities){
        List<CategoryEntity> menus =  categoryEntities.stream().filter(categoryEntity -> {
                return categoryEntity.getParentCid().equals(menu.getCatId());
        }).peek(m -> {
            m.setChildren(getChildren(m,categoryEntities));
        }).collect(Collectors.toList());
        return menus;
    }

}